﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace FaultBackService.Shared
{
    public partial class Log
    {
        string _logPath;
        private object thisLock = new object();

        public Log()
        {
            _logPath = @"D:/TurbineFault_Log/Log/log_" + DateTime.Today.ToString("yyyyMMdd") + ".txt";

            var directoryInfo = @"D:/TurbineFault_Log/Log";
            if (!Directory.Exists(directoryInfo)) Directory.CreateDirectory(directoryInfo);
        }


        private void WriteOneLine(string message)
        {
            try
            {
                //Console.WriteLine(DateTime.Now + "\t" + message);
#if DEBUG
                Console.WriteLine(DateTime.Now + "\t" + message);
#endif
                lock (thisLock)
                {
                    using (FileStream fs = File.Open(_logPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
                    {
                        string msg = DateTime.Now + "\t" + message + "\r\n";
                        //byte[] bs = new UTF8Encoding().GetBytes(msg);
                        byte[] bs = Encoding.GetEncoding("gb2312").GetBytes(msg);
                        fs.Write(bs, 0, bs.Length);
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

        }



        /* 实时故障实时分析 */
        //开始连接SCADA数据库
        public void StartConnectSCADA()
        {
            WriteOneLine(@"开始连接SCADA数据库，读取最新十分钟数据时间...");
        }


        /// <summary>
        /// 连接故障数据库成功
        /// </summary>
        public void ConnectFaultSuccess()
        {
            WriteOneLine(@"连接故障数据库成功.");
        }


        /// <summary>
        /// 开始新的实时故障实时分析Timer
        /// </summary>
        public void StartNewTimerForRealTimeFault()
        {
            WriteOneLine(@"开始新的实时故障实时分析Timer");
        }

        /// <summary>
        /// 开始新的实时故障每日分析Timer
        /// </summary>
        public void StartNewTimerForRealTimeFaultDaily()
        {
            WriteOneLine(@"开始新的实时故障每日分析Timer");
        }

        /// <summary>
        /// 结束当前实时故障实时分析Timer
        /// </summary>
        public void EndTimerForRealTimeFault()
        {
            WriteOneLine(@"结束当前实时故障实时分析Timer");
        }



        public void ReadSCADASuccess2(string dataTime, int count)
        {
            WriteOneLine(@"Read SCADA success,TimeStamp = " + dataTime + @",Number = " + count);
        }

        public void ReadSCADAFail(string message)
        {
            WriteOneLine(@"SCADA数据读取失败." + message.Split('。')[0]);
        }

        /// <summary>
        /// 实时数据分析发生错误
        /// </summary>
        /// <param name="message"></param>
        public void RealTimeFaultError(string message)
        {
            WriteOneLine(@"十分钟数据实时分析发生错误." + message.Split('。')[0]);
        }

        /// <summary>
        /// 实时数据分析完成
        /// </summary>
        public void RealTimeFaultSuccess()
        {
            WriteOneLine(@"十分钟数据实时分析完成.");
        }

        /// <summary>
        /// 趋势数据每日分析发生错误
        /// </summary>
        /// <param name="message"></param>
        public void TrendError(string message)
        {
            WriteOneLine(@"趋势分析发生错误." + message.Split('。')[0]);
        }



        /// <summary>
        /// 实时故障每日分析发生错误
        /// </summary>
        /// <param name="message"></param>
        public void RealTimeFaultDailyError(string message)
        {
            WriteOneLine(@"实时故障每日分析发生错误." + message.Split('。')[0]);
        }



        public void DataIsOut()
        {
            WriteOneLine(@"SCADA数据已过时，数据未刷新.");
        }

        public void ConnectFaultDatabaseFail(string message)
        {
            WriteOneLine(@"故障数据库连接失败." + message.Split('。')[0]);
        }


        internal void WriteFarmMean(int farmMeanId)
        {
            WriteOneLine(@"Write into TenMeanFarmData success,Identity=" + farmMeanId);
        }

        internal void WriteRealFault(int faultCount)
        {
            WriteOneLine(@"向RealTimeFaultData中写入" + faultCount + "条记录.");
        }





        internal void GetScadaLastTenMeanTimeSuccess(DateTime lastTenMeanTimeOfScada)
        {
            WriteOneLine(@"成功连接SCADA数据库，十分钟数据最新时间为：" + lastTenMeanTimeOfScada + @"，开始周期读取十分钟数据.");
        }

        internal void DataIsEmpty()
        {
            WriteOneLine(@"SCADA十分钟数据表为空");
        }

        internal void DataIsEmpty2(string time)
        {
            WriteOneLine(@"本十分钟周期无SCADA数据，数据时间为："+time);
        }



        public void StartChecktime()
        {
            WriteOneLine(@"开始对时......");
        }

        public void ChecktimeCompleted(string dbTime)
        {
            WriteOneLine(@"对时完成，数据库时间为:" + dbTime + "，开始进行数据读取.");
        }

        public void StartCycleRead()
        {
            WriteOneLine(@"开始周期数据读取......");
        }

        public void ReadSCADASuccess(string dbTime)
        {
            WriteOneLine(@"SCADA数据读取成功，数据库时间为:" + dbTime);
        }
        
    }

    /// <summary>
    /// 实时故障实时分析相关LOG
    /// </summary>
    public partial class Log
    {
        public void TenMeanRealTime_ReadLastSCADATimeSuccess(string time)
        {
            WriteOneLine(@"读取最新SCADA数据时间成功，时间为:" + time);
        }

        public void TenMeanRealTime_ReadLastSCADATimeError(string msg)
        {
            WriteOneLine(@"读取最新SCADA数据时间错误:" + msg.Split('。')[0]);
        }
    }

    /// <summary>
    /// 实时故障每日分析相关LOG
    /// </summary>
    public partial class Log
    {

        //开始连接实时故障数据库
        public void StartConnectFault()
        {
            WriteOneLine(@"开始连接实时故障数据库...");
        }

        /// <summary>
        /// 连接故障数据库失败
        /// </summary>
        public void ConnectFaultFail(string message)
        {
            WriteOneLine(@"连接故障数据库失败." + message.Split('。')[0]);
        }

        public void RealTimeFaultDailyStart()
        {
            WriteOneLine(@"开始当天的实时故障每日分析...");
        }

        public void RealTimeFaultDailySuccess(int count)
        {
            WriteOneLine(@"实时故障每日分析执行成功，写入记录" + count + "条.");
        }

        internal void RealTimeFaultDailyStop()
        {
            WriteOneLine(@"前一日实时故障数目为0，实时故障每日分析中止执行.");
        }
    }

    /// <summary>
    /// 趋势分析相关LOG
    /// </summary>
    public partial class Log
    {
        public void Trend_ReadLastDateSuccess(string time)
        {
            WriteOneLine(@"读取最新趋势数据时间成功，时间为:" + time);
        }

        public void Trend_ReadLastDateError(string msg)
        {
            WriteOneLine(@"读取最新趋势数据时间错误:" + msg.Split('。')[0]);
        }

        /// <summary>
        /// 趋势数据每日分析完成
        /// </summary>
        public void TrendSuccess()
        {
            WriteOneLine(@"趋势分析完成并写入数据库.");
        }

        public void Trend_WriteCount(string s,int n)
        {
            WriteOneLine(@"趋势分析: "+s+",向数据库写入"+n+"条稳定值.");
        }
    }
}
